package com.code_g.service.Impl;

import com.code_g.dao.Impl.UserDaoImpl;
import com.code_g.dao.UserDao;
import com.code_g.domain.PageBean;
import com.code_g.domain.User;
import com.code_g.service.UserService;

import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
    private UserDao dao = new UserDaoImpl();

    @Override
    public List<User> findAll() {
        return dao.findAll();
    }

    @Override
    public User login(User user) {
        return dao.login(user);
    }

    @Override
    public void addUser(User user) {
        dao.addUser(user);
    }

    @Override
    public void deleteUser(int id) {
        dao.deleteUser(id);
    }

    @Override
    public User findUser(int id) {
        return dao.findUser(id);
    }

    @Override
    public void updateUser(User user) {
        dao.updateUser(user);
    }

    @Override
    public void delSelect(String[] ids) {
        dao.delSelect(ids);
    }

    @Override
    public PageBean<User> findUserBypage(String currentPage, String rows, Map<String, String[]> map) {
        PageBean<User> pageBean = new PageBean<>();
        int page = Integer.parseInt(currentPage);
        int rownum = Integer.parseInt(rows);
        int count = dao.findTotalCount(map);
        int start = (page - 1) * rownum;
        List<User> users = dao.findByPage(start, rownum, map);
        int pagenum = count % rownum == 0 ? count / rownum : count / rownum + 1;
        pageBean.setCurrentPage(page);
        pageBean.setRows(rownum);
        pageBean.setList(users);
        pageBean.setTotalCount(count);
        pageBean.setTotalPage(pagenum);
        return pageBean;
    }


}
